﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MovieStorage;

namespace DVDFilmoteka.Sorting
{
    class HodnoceniSorter : ISorter
    {
        private readonly List<String> expanded = new List<string>();

        #region ISorter Members
                
        public void Sort(TreeView treeView, List<MovieDataStore12> movieList)
        {
            List<String> hodnoceniNodeList = Tools.loadDataFromXML(Tools.CONFIG, "//root/mhodnoceni/*");
            hodnoceniNodeList.Reverse();

            // Smazat
            treeView.Nodes.Clear();

            // Pridat uzli s pocatcnimy pismeny
            foreach (var item in hodnoceniNodeList)
            {
                treeView.Nodes.Add(item);
            }

            // Naplnit uzli nazvy filmu
            foreach (var item in movieList)
            {
                int index = 0;
                String name = Properties.Messages.messages_nomoviename;
                if (!string.IsNullOrEmpty(item.czechName))
                {
                    index = hodnoceniNodeList.IndexOf(item.myRating);
                    name = item.czechName;
                }
                else if (!string.IsNullOrEmpty(item.originalName))
                {
                    index = hodnoceniNodeList.IndexOf(item.myRating);
                    name = item.originalName;
                }

                if (index < 0 || index >= treeView.Nodes.Count) index = 0;

                TreeNode node = new TreeNode(name);
                node.Tag = item; // V treelistu si drzim ukazatel na data o filmu

                treeView.Nodes[index].Nodes.Add(node);
            }

            // Vymazat uzli, ktere jsou prazdne
            for (int i = 0; i < treeView.Nodes.Count; i++)
            {
                TreeNode item = treeView.Nodes[i];
                if (item.Nodes == null || item.Nodes.Count == 0)
                {
                    treeView.Nodes.Remove(item);
                    i--;
                }
            }

            // Expanduje korenove nody pokud byli predtim expandovane
            if (expanded != null && expanded.Count > 0)
            {
                foreach (TreeNode item in treeView.Nodes)
                {
                    if (expanded.Contains(item.Text))
                    {
                        item.Expand();
                    }
                }
            }
        }

        public void SaveExpanded(TreeView treeView)
        {
            expanded.Clear();
            foreach (TreeNode item in treeView.Nodes)
            {
                if (item.IsExpanded)
                {
                    expanded.Add(item.Text);
                }
            }
        }

        public ESorters Category()
        {
            return ESorters.HODNOCENI;
        }

        public string Name()
        {
            return Properties.Messages.sorter_HODNOCENI;
        }

        #endregion
    }
}
